// pages/goods_detail/goods_detail.js
import { request } from "../../request/index.js";
import { showModal, showToast, uploadFile, chooseImage, showLoading } from "../../util/AsyncWx.js";
var app = getApp();
let a = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: {},
    discussInfo: {},
    goodsid: '',
    allDiscuss: {},
    collectgoods: {},
    MyCollectedGoods: [],
    isCollect: false,
    isLongpress: false,
    isfocus: false,
    uid: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (a) {
      a = false;
      return
    } else {
      var that = this
      let pages = getCurrentPages();
      let currentPage = pages[pages.length - 1];
      let options = currentPage.options;
      const { id } = options;
      const uid = app.globalData.uid
      that.setData({
        uid: uid,
        goodsid: id,
        collectgoods: {
          gid: id,
          uid: app.globalData.uid
        }
      })
      that.getGoodsDetail(id);
      that.getGoodsReplyAll(id);
      that.getCollectGoodsByUid();
    }
  },
  longpress(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    that.setData({
      isLongpress: true,
      currentid: id
    })
    wx.showActionSheet({
      itemList: ['删除评论'],
      itemColor: '#000000',
      success: (result) => {
        that.deleteReply(id)
      },
      complete: () => {
        that.setData({
          isLongpress: false
        })
      }
    });
  },
  async getCollectGoodsByUid() {
    var id = app.globalData.uid
    var that = this
    const result = await request({ url: "/collectgoods/getAllCollectGoods", data: { id } })
    that.setData({
      MyCollectedGoods: result
    })
    var MyCollectedGoods = that.data.MyCollectedGoods
    var goodsInfo = that.data.goodsInfo
    let index = MyCollectedGoods.findIndex(v => v.gid === that.data.goodsInfo[0].id);
    if (index !== -1) {
      that.setData({
        isCollect: true
      })
    } else {
      that.setData({
        isCollect: false
      })
    }

  },

  async getGoodsDetail(id) {
    var that = this
    const result = await request({ url: "/goods/getGoodsById", data: { id } })
    that.setData({
      goodsInfo: result
    })
  },
  handleImagePreview: function (e) {
    a = true
    var that = this
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url],
    });

  },
  //获取输入的评论值
  discussInput: function (e) {
    var that = this
    that.setData({
      discussInfo: {
        content: e.detail.value,
        uid: app.globalData.uid,
        goodsid: that.data.goodsid,
      }
    })
  },
  async discussSend() {
    var that = this
    var discussInfo = that.data.discussInfo
    var content = that.data.discussInfo.content
    var id = that.data.discussInfo.goodsid
    if (!content) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        duration: 2000,
        mask: false,
      });
      return false;

    } else {
      const result = await request({ url: "/goodsReply/addGoodsReply", method: "POST", data: discussInfo })
      var id = that.data.goodsid
      that.setData({
        discussInfo: ''
      })
      that.getGoodsReplyAll(id)

    }
  },
  async getGoodsReplyAll(id) {
    var that = this
    const res = await request({ url: "/goodsReply/getGoodsReplyById", data: { id } })
    that.setData({
      allDiscuss: res
    })
  },
  async deleteReply(id) {
    var that = this
    const res = await request({ url: "/goodsReply/deleteReplyById", data: { id } })
    var id = that.data.goodsid
    that.getGoodsReplyAll(id);
  },
  async handlecollect() {
    var that = this
    var MyCollectedGoods = that.data.MyCollectedGoods
    var goodsInfo = that.data.goodsInfo
    if (that.data.isCollect === false) {
      var collectgoods = that.data.collectgoods
      const res = await request({ url: "/collectgoods/addCollectGoods", method: "POST", data: collectgoods })
      that.getCollectGoodsByUid()

    } else {
      var gid = that.data.goodsid
      var uid = app.globalData.uid
      const res1 = await request({ url: "/collectgoods/deleteCollectGoodsById", data: { gid, uid } })
      that.getCollectGoodsByUid()
    }


  },
  showInput() {
    this.setData({
      isfocus: true
    })
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})